//
//  SEGKitHelper.m
//  ExcellentHome
//
//  Created by 刘山 on 2017/11/13.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGKitHelper.h"
#import "SEGText.h"

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif

@implementation SEGKitHelper
+ (NSMutableAttributedString *)seg_TextWithString:(NSString *)textString fontSize:(CGFloat)fontSize textColor:(UIColor *)textColor {
    return [SEGKitHelper seg_TextWithString:textString fontSize:fontSize textColor:textColor lineSpacing:0];
}

+ (CGSize)seg_sizeWithString:(NSString *)textString
                       fontSize:(CGFloat)fontSize
                      textColor:(UIColor *)textColor {
    NSMutableAttributedString *text = [SEGKitHelper seg_TextWithString:textString fontSize:fontSize textColor:textColor lineSpacing:0];
    
    return [text boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
}


+ (NSMutableAttributedString *)seg_TextWithString:(NSString *)textString fontSize:(CGFloat)fontSize textColor:(UIColor *)textColor  lineSpacing:(CGFloat)lineSpacing{
    if (!textString) return nil;
    NSMutableString *string = textString.mutableCopy;
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:string];
    text.yy_font = [UIFont systemFontOfSize:fontSize];
    text.yy_color = textColor;
    text.yy_lineSpacing = lineSpacing;
    return text;
}

+ (NSMutableAttributedString *)seg_textWithString:(NSString *)textString
                                   topicString:(NSString *)topicString
                                      fontSize:(CGFloat)fontSize
                                     textColor:(UIColor *)textColor {
    if (!textString) return nil;
    NSMutableString *string = textString.mutableCopy;
    
    // 字体
    UIFont *font = [UIFont systemFontOfSize:fontSize];
    // 高亮状态的背景
    SEGTextBorder *highlightBorder = [SEGTextBorder new];
    highlightBorder.insets = UIEdgeInsetsMake(-2, 0, -2, 0);
    highlightBorder.cornerRadius = 3;
    highlightBorder.fillColor = kColor_1;
    
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:string];
    text.yy_font = font;
    text.yy_color = textColor;
    
    // 根据 topicStruct 中每个 Topic.topicTitle 来匹配文本，标记为话题
    if (topicString.length != 0){
        NSString *topicTitle = [NSString stringWithFormat:@"#%@#",topicString];
        NSRange searchRange = NSMakeRange(0, text.string.length);
        NSRange range = [text.string rangeOfString:topicTitle options:kNilOptions range:searchRange];
        if (range.location != NSNotFound){
            
            if ([text yy_attribute:SEGTextHighlightAttributeName atIndex:range.location] == nil) {
                [text yy_setColor:kColor_S3 range:range];
                // 高亮状态
                SEGTextHighlight *highlight = [SEGTextHighlight new];
                [highlight setBackgroundBorder:highlightBorder];
                // 数据信息，用于稍后用户点击
                highlight.userInfo = @{@"TOPIC" : topicString};
                [text yy_setTextHighlight:highlight range:range];
            }
            
            searchRange.location = searchRange.location + (searchRange.length ? searchRange.length : 1);
            if (searchRange.location + 1>= text.length){
                
            }else{
                searchRange.length = text.length - searchRange.location;
            }
        }
    }
    return text;
}

+ (NSAttributedString *)seg_attachmentWithFontSize:(CGFloat)fontSize image:(UIImage *)image shrink:(BOOL)shrink {

    // Heiti SC 字体。。
    CGFloat ascent = fontSize * 0.86;
    CGFloat descent = fontSize * 0.14;
    CGRect bounding = CGRectMake(0, -0.14 * fontSize, fontSize, fontSize);
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(ascent - (bounding.size.height + bounding.origin.y), 0, descent + bounding.origin.y, 0);
    
    SEGTextRunDelegate *delegate = [SEGTextRunDelegate new];
    delegate.ascent = ascent;
    delegate.descent = descent;
    delegate.width = bounding.size.width;
    
    SEGTextAttachment *attachment = [SEGTextAttachment new];
    attachment.contentMode = UIViewContentModeScaleAspectFit;
    attachment.contentInsets = contentInsets;
    attachment.content = image;
    
    if (shrink) {
        // 缩小~
        CGFloat scale = 1 / 10.0;
        contentInsets.top += fontSize * scale;
        contentInsets.bottom += fontSize * scale;
        contentInsets.left += fontSize * scale;
        contentInsets.right += fontSize * scale;
        contentInsets = contentInsets;
        attachment.contentInsets = contentInsets;
    }
    
    NSMutableAttributedString *atr = [[NSMutableAttributedString alloc] initWithString:SEGTextAttachmentToken];
    [atr yy_setTextAttachment:attachment range:NSMakeRange(0, atr.length)];
    CTRunDelegateRef ctDelegate = delegate.CTRunDelegate;
    [atr yy_setRunDelegate:ctDelegate range:NSMakeRange(0, atr.length)];
    if (ctDelegate) CFRelease(ctDelegate);
    
    return atr;
}

+ (CGFloat)seg_heightForOneLineWIthFontSize:(CGFloat)fontSize{
    NSMutableAttributedString *text = [self seg_TextWithString:@"测试" fontSize:fontSize textColor:nil];
    return [text boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size.height;
}

/**
 * 图片转成富文本
 * @author Sam
 * @param img  图片
 * @param fontSize  富文本字体大小
 */
+ (NSAttributedString *)segAttachmentWithImage:(UIImage*)img FontSize:(CGFloat)fontSize{
    return [SEGKitHelper segAttachmentWithImage:img FontSize:fontSize contentInsets:UIEdgeInsetsMake(0,0,0,0)];
}

/**
 * 图片转成富文本
 * @author Sam
 * @param img  图片
 * @param fontSize  富文本字体大小
 * @param contentInsets  间距大小
 */
+ (NSAttributedString *)segAttachmentWithImage:(UIImage*)img FontSize:(CGFloat)fontSize contentInsets:(UIEdgeInsets)contentInsets{
    
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    [attributes setObject:[UIFont systemFontOfSize:fontSize] forKey:NSFontAttributeName];
    [attributes setObject:[UIColor clearColor] forKey:NSBackgroundColorAttributeName];
    
    CGFloat ascent = fontSize * 0.86;
    CGFloat descent = fontSize * 0.14;
    
    //(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
    
    SEGTextRunDelegate *delegate = [SEGTextRunDelegate new];
    delegate.ascent = ascent;
    delegate.descent = descent;
    delegate.width = img.size.width + contentInsets.left + contentInsets.right;
    
    SEGTextAttachment *attachment = [[SEGTextAttachment alloc]init];
    attachment.content = img;
    attachment.contentInsets = contentInsets;
    
    NSMutableAttributedString *atr = [[NSMutableAttributedString alloc] initWithString:SEGTextAttachmentToken];
    [atr yy_setTextAttachment:attachment range:NSMakeRange(0, atr.length)];
    CTRunDelegateRef ctDelegate = delegate.CTRunDelegate;
    [atr yy_setRunDelegate:ctDelegate range:NSMakeRange(0, atr.length)];
    if (ctDelegate) CFRelease(ctDelegate);
    return atr;
}
@end
